<template>
  <view class="login-content">
  </view>
</template>

<script>
import mRequest from "@/api/home"
import hccmStore from "@/common/hccm-store.js"
import hccmConfig from "@/common/hccm-config.js"
export default {
  
  data () {
    return {
      action: "", //
      phone: "", //
      hospId: '',
      prescriptionIds: '',
    }
  },
  onLoad (options) {
    uni.showLoading({
      title: "正在加载...",
    })
    this.phone = options.phone || '15566356668'
    // 18613856809   18613856809 18642609403 18018962168
    this.prescriptionIds = options.prescriptionId || ''
    this.action = options.action || ''
    this.hospId = options.hospId || ''

    // 13470076278 测试地址的账号 13256094967
    if(this.phone){
      hccmStore.setUserPhone(this.phone)
      this.getToken()
      if(this.hospId){
        hccmStore.setUserHospId(this.hospId)
      }
    }else{
      this.toUrlByAction()
    }
    // if (hccmStore.getUser() && hccmStore.getUserPhone()) {
    //   //已经有用户信息
    //   uni.hideLoading()
    //   this.toUrlByAction()
    // } else {
    //   this.getToken()
    //   uni.login({
    //     success: res => {
    //       this.getOpenId(res.code)
    //     },
    //   })
    // }
  },
  methods: {
    // 通过手机号获取token
    getToken () {
      let param = {
        phone: this.phone,
      }
      mRequest.getToken(param).then(
        res => {
          hccmStore.setPhoneToken(res.responseData)
          uni.login({
            success: res => {
              this.getOpenId(res.code)
            },
          })
        },
        error => { },
      )
    },
    // 获取openId
    getOpenId (code) {
      let param = {
        code: code,
      }
      mRequest.getOpenId(param).then(
        res => {
          hccmStore.setWxOpenId(res.responseData)
          // this.getPatient()
          if (this.action == 'order') {
            this.toUrlByAction()
          } else {
            this.getPatient()
          }
        },
        error => { },
      )
    },
    getPatient () {
      if(hccmStore.getUserPhone()){
        let param = {
          phoneNo: hccmStore.getUserPhone(),
        }
        mRequest.getPatient(param).then(res => {
          hccmStore.setUser(res.responseData)
          uni.hideLoading()
          this.toUrlByAction()
        })
      }
    },

    // TODO 需要判断进start页面得时候进哪个页面，现在暂时是home-main
    toUrlByAction () {
      //
      if (this.action == "order") {
        uni.redirectTo({
          //关闭当前页面，跳转到应用内的某个页面。
          url: `/pages/order/order?prescriptionId=${this.prescriptionIds}&hospId=${this.hospId}&action=${this.action}`,
        });
      } else if (this.action == "orderList") {
        console.log('action', this.action, this.phone)
        uni.redirectTo({
          //关闭当前页面，跳转到应用内的某个页面。
          url: `/pages/order-list/order-list?hospId=${this.hospId}`
        });
      } else if (this.action == "list") {
        console.log('action', this.action, this.phone)
        uni.redirectTo({
          //关闭当前页面，跳转到应用内的某个页面。
          url: `/pages/home-main/home-main?hospId=${this.hospId}`
        });
      } else {
        uni.redirectTo({
          //关闭当前页面，跳转到应用内的某个页面。
          url: `/pages/home-main/home-main`
        });
      }
    },
  },
}
</script>

<style lang="scss">
.login-content {
  background-color: #fff;
}
</style>
